package util

import (
	"fmt"
	"regexp"
	"strings"
	"time"
)

var (
	DefaultDateFormat       = "2006-01-02"
	DefaultDatetime   		= "2006.01.02 15:04:05"
	DefaultDatetimeFormat   = "2006-01-02 15:04:05"
	DefaultDatetimeFormat1  = "2006-01-02 15:04"
	DefaultDatetimeFormat2  = "2006-01-02 15"
	FormatYYMMDDHH          = "2006010215"
	FormatYYMMDD            = "20060102"
	DefaultDateFormatZh     = "2006年1月2日"
	DefaultDatetimeFormatZh = "2006年1月2日 15时04分05秒"
	FormatYmdHis            = "20060102150405"
)


// 固定格式转换成时间戳
func DatetimeParseLocal(t string) int64 {
	loc, _ := time.LoadLocation("Local")
	tmp, _ := time.ParseInLocation(DefaultDatetimeFormat, t, loc)
	return tmp.Unix()
}

// 格式转换成时间戳
func DatetimeParseFormat(t string,format string) int64 {
	loc, _ := time.LoadLocation("Local")
	tmp, _ := time.ParseInLocation(format, t, loc)
	return tmp.Unix()
}

func FrontTime(us int64) string {
	return time.Unix(us, 0).Format(DefaultDatetime)
}

// 秒
func SeTime() int64 {
	return time.Now().Unix()
}

// 毫秒
func MsTime() int64 {
	return time.Now().UnixNano() / 1e6
}


func TimeToUnix(t string) (int64,error){
	// 判断时间格式
	// 1d 2d 默认n天后对应当前整点
	// 1h 2h 默认n小时后当前分钟
	// 1m 2m n月后当前整点
	// 2002-05-22 11:11:11
	// 2002-05-22 11:11
	// 2002-05-22 11
	// 2002-05-22
	// 20020522
	// 0511

	rm,_ := regexp.Compile("^[0-9]+m$") // 月
	rd,_ := regexp.Compile("^[0-9]+d$") // 日
	rh,_ := regexp.Compile("^[0-9]+h$") // 小时
	rtimescend,_ := regexp.Compile("^[0-9]{4}-[0,1][0-9]-[0-2][0-9] [0,1,2][0-9]:[0-5][0-9]:[0-5][0-9]$") // 精确秒
	rtimeminutes,_ := regexp.Compile("^[0-9]{4}-[0,1][0-9]-[0-2][0-9] [0,1,2][0-9]:[0-5][0-9]$") // 精确分
	rtimehour,_ := regexp.Compile("^[0-9]{4}-[0,1][0-9]-[0-2][0-9] [0,1,2][0-9]$") // 精确时
	rtimeday1,_ := regexp.Compile("^[0-9]{4}-[0,1][0-9]-[0-2][0-9]$") // 精确天
	rtimeday2,_ := regexp.Compile("^[0-9]{4}[0,1][0-9][0-2][0-9]$") // 精确天
	rtimeday3,_ := regexp.Compile("^[0,1][0-9][0-2][0-9]$") // 精确天


	if rm.MatchString(t) {
		n := strings.Replace(t,"m","",-1)
		returnTime := time.Now().AddDate(0,StrToInt(n),0).Unix()
		return returnTime,nil
	} else if rd.MatchString(t) {
		n := strings.Replace(t,"d","",-1)
		returnTime := time.Now().AddDate(0,0,StrToInt(n)).Unix()
		return returnTime,nil
	} else if rh.MatchString(t) {
		n := strings.Replace(t,"h","",-1)
		fmt.Println(n)
		returnTime := time.Now().Unix()+int64(StrToInt(n)*60*60)
		return returnTime,nil
	} else if rtimescend.MatchString(t) {
		returnTime := DatetimeParseFormat(t,DefaultDatetimeFormat)
		return returnTime,nil
	} else if rtimeminutes.MatchString(t) {
		returnTime := DatetimeParseFormat(t,DefaultDatetimeFormat1)
		return returnTime,nil
	} else if rtimehour.MatchString(t) {
		returnTime := DatetimeParseFormat(t,DefaultDatetimeFormat2)
		return returnTime,nil
	} else if rtimeday1.MatchString(t) {
		returnTime := DatetimeParseFormat(t,DefaultDateFormat)
		return returnTime,nil
	} else if rtimeday2.MatchString(t) {
		returnTime := DatetimeParseFormat(t,FormatYYMMDD)
		return returnTime,nil
	} else if rtimeday3.MatchString(t) {
		returnTime := DatetimeParseFormat(IntToStr(time.Now().Year()) + t,FormatYYMMDD)
		return returnTime,nil
	}

	return 0,Err{"格式不匹配"}
}


